<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=6.7.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.7.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/uploads/favicon.ico?v=6.7.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/uploads/favicon.ico?v=6.7.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.7.0" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.7.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":true},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="PHP是世界上最伟大的语言，这一点是毋庸置疑的吧。哈哈哈哈哈哈 。这个霸气的开头不错！(^__^) 但是正是因为伟大，所以用的人也就多了，人一多，再牛逼再伟大的东西，都会产生问题，逐渐就造成了很多的分歧。每个人的习惯不一样，所以在一些PHP语法上的一些表现也是千差万别，比如这些问题：  function 函数名是否驼峰命名，花括号{}是否换行写，是该写注释呢还是不写变量名是大写还是小写，一个php">
<meta name="keywords" content="php规范,php代码规范,PSR0-4">
<meta property="og:type" content="article">
<meta property="og:title" content="「php」php笔记-PSR[0-4]规范(转载)">
<meta property="og:url" content="/2018/11/28/【php】php笔记-PSR0-4规范/index.html">
<meta property="og:site_name" content="吴楚衡">
<meta property="og:description" content="PHP是世界上最伟大的语言，这一点是毋庸置疑的吧。哈哈哈哈哈哈 。这个霸气的开头不错！(^__^) 但是正是因为伟大，所以用的人也就多了，人一多，再牛逼再伟大的东西，都会产生问题，逐渐就造成了很多的分歧。每个人的习惯不一样，所以在一些PHP语法上的一些表现也是千差万别，比如这些问题：  function 函数名是否驼峰命名，花括号{}是否换行写，是该写注释呢还是不写变量名是大写还是小写，一个php">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-04-10T13:09:57.171Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="「php」php笔记-PSR[0-4]规范(转载)">
<meta name="twitter:description" content="PHP是世界上最伟大的语言，这一点是毋庸置疑的吧。哈哈哈哈哈哈 。这个霸气的开头不错！(^__^) 但是正是因为伟大，所以用的人也就多了，人一多，再牛逼再伟大的东西，都会产生问题，逐渐就造成了很多的分歧。每个人的习惯不一样，所以在一些PHP语法上的一些表现也是千差万别，比如这些问题：  function 函数名是否驼峰命名，花括号{}是否换行写，是该写注释呢还是不写变量名是大写还是小写，一个php">






  <link rel="canonical" href="/2018/11/28/【php】php笔记-PSR0-4规范/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>「php」php笔记-PSR[0-4]规范(转载) | 吴楚衡</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta custom-logo">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">吴楚衡</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">来找我吧！我的目标是星辰大海！</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档<span class="badge">99</span></a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签<span class="badge">77</span></a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类<span class="badge">17</span></a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    
      
    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-历史">

    
    
    
      
    

    
      
    

    <a href="/history/" rel="section"><i class="menu-item-icon fa fa-fw fa-history"></i> <br>历史</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-书单">

    
    
    
      
    

    
      
    

    <a href="/book-list/index.html" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i> <br>书单</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-工具">

    
    
    
      
    

    
      
    

    <a href="/tool-list/index.html" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i> <br>工具</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  
    

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="/2018/11/28/【php】php笔记-PSR0-4规范/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="吴楚衡">
      <meta itemprop="description" content="IT菜鸟,写这个博客是防止未来把自己学习的东西给忘记了,也是为了自己学习的那点东西做下总结加深下印象,请多指教^_^!">
      <meta itemprop="image" content="/uploads/favicon.ico">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="吴楚衡">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">「php」php笔记-PSR[0-4]规范(转载)<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL3d1Y2h1aGVuZy9oZXhvL3RyZWUvbWFzdGVyL3NvdXJjZS9fcG9zdHMvX3Bvc3RzL+OAkHBocOOAkXBocOeslOiusC1QU1IwLTTop4TojIMubWQ=" title="编辑"><i class="fa fa-pencil"></i></span>

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-11-28 21:28:46" itemprop="dateCreated datePublished" datetime="2018-11-28T21:28:46+08:00">2018-11-28</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-04-10 21:09:57" itemprop="dateModified" datetime="2020-04-10T21:09:57+08:00">2020-04-10</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/php/" itemprop="url" rel="index"><span itemprop="name">php</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <a href="/2018/11/28/【php】php笔记-PSR0-4规范/#comments" itemprop="discussionUrl">
                  <span class="post-meta-item-text">评论数：</span> <span class="post-comments-count valine-comment-count" data-xid="/2018/11/28/【php】php笔记-PSR0-4规范/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon">
            <i class="fa fa-eye"></i>
             阅读次数： 
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><code>PHP</code>是世界上最伟大的语言，这一点是毋庸置疑的吧。哈哈哈哈哈哈 。这个霸气的开头不错！(^__^)</p>
<p>但是正是因为伟大，所以用的人也就多了，人一多，再牛逼再伟大的东西，都会产生问题，逐渐就造成了很多的分歧。每个人的习惯不一样，所以在一些<code>PHP</code>语法上的一些表现也是千差万别，比如这些问题：</p>
<blockquote>
<p><code>function</code> 函数名是否驼峰命名，<br>花括号{}是否换行写，<br>是该写注释呢还是不写<br>变量名是大写还是小写，<br>一个php文件中是一个类好还是允许有多个</p>
</blockquote>
<hr>
<a id="more"></a>
<p>等等一系列的问题，接踵而至，你说你的这种方式屌，我说你傻逼啊我的才好。所以。各种争执声此起彼伏。<br>特别是当你去接手上任留下的坑的时候，如果你有自己的规范或者你是处女座，你就会骂死那个狗日的一万遍了，每改一次就会改一遍骂：“麻痹的写的都是什么狗shit烂代码啊”。所以，在<code>PHP</code>程序员的心中一个共有的心愿诞生了：“能不能他妈的大家都遵守同一规范啊”。<br>好，既然大家都有共同的心声，那么这样一个的公共的规范就出来了，他就是<code>PSR-[0-4]</code>规范，它出来后，大家都觉得挺不错的啊，还蛮符合自己的书写风格的，渐渐的大家都知道了它，也慢慢的接受了它，越来越多的人知道了它，一些大牛的项目都慢慢的遵守了这个规范，然后也一帮菜鸟一看大牛都在用这个规范，我为啥不也尝试着规范一下呢？<br>所以，渐渐的<code>PSR-[0-4]</code>规范成为了<code>PHP</code>的一个王牌规范，你写<code>PHP</code>就最好必须按照这里面的规范写，全球共享好代码。</p>
<h2 id="PHP-FIG"><a href="#PHP-FIG" class="headerlink" title="PHP-FIG"></a>PHP-FIG</h2><p>在说啥是<code>PSR-[0-4]</code>规范的之前，我觉得我们有必要说下它的发明者和规范者：<code>PHP-FIG</code>，它的网站是：<span class="exturl" data-url="aHR0cDovL3d3dy5waHAtZmlnLm9yZ+OAguWwseaYr+i/meS4quiBlOebn+e7hOe7h+WPkeaYjuWSjOWIm+mAoOS6hlBTUi1bMC00XeinhOiMg++8jOiGnOaLnOWQp++8jOWxjOS4neS7rO+8gQ==" title="http://www.php-fig.org。就是这个联盟组织发明和创造了PSR-[0-4]规范，膜拜吧，屌丝们！">www.php-fig.org。就是这个联盟组织发明和创造了PSR-[0-4]规范，膜拜吧，屌丝们！<i class="fa fa-external-link"></i></span></p>
<p>FIG 是 Framework Interoperability Group（框架可互用性小组）的缩写，由几位开源框架的开发者成立于 2009 年，从那开始也选取了很多其他成员进来，虽然不是 “官方” 组织，但也代表了社区中不小的一块。组织的目的在于：以最低程度的限制，来统一各个项目的编码规范，避免各家自行发展的风格阻碍了程序设计师开发的困扰，于是大伙发明和总结了<code>PSR</code>，<code>PSR</code>是Proposing a Standards Recommendation（提出标准建议）的缩写，截止到目前为止，总共有5套PSR规范，分别是：</p>
<blockquote>
<p>PSR-0 (Autoloading Standard) 自动加载标准<br>PSR-1 (Basic Coding Standard) 基础编码标准<br>PSR-2 (Coding Style Guide) 编码风格向导<br>PSR-3 (Logger Interface) 日志接口<br>PSR-4 (Improved Autoloading) 自动加载的增强版，可以替换掉PSR-0了。</p>
</blockquote>
<hr>
<p>接下来的篇目，我们会针对这5套，深入了解下。仔细学习下受万千<code>PHPer</code>热捧的这5套规范到底有啥出众之处。</p>
<h2 id="0-0-PSR-0-规范"><a href="#0-0-PSR-0-规范" class="headerlink" title="0.0 PSR-0 规范"></a>0.0 PSR-0 规范</h2><p>PRS-0规范是他们出的第1套规范，主要是制定了一些自动加载标准（Autoloading Standard），如果你英文比较好，可以直接看官网的这个：PSR-0，很短。我们打开PSR-0的主页时，发现多了一个警告：</p>
<blockquote>
<p>Deprecated - As of 2014-10-21 PSR-0 has been marked as deprecated. PSR-4 is now recommended as an alternative.</p>
</blockquote>
<hr>
<p>大概意思：</p>
<blockquote>
<p>不推荐使用 - 在2014年10月21日PSR-0已被标记为过时。PSR-4现在推荐作为替代。</p>
</blockquote>
<hr>
<p>那么也就是说PSR-0已！经！过！时！了！，别哭，<code>PHP</code>代码狗。虽说已经过时，但是我们也可以看看嘛，这不影响我们的学习，好了。废话不说了，开始吧：<br>PSR-0强制性要求几点：</p>
<blockquote>
<ul>
<li>一个完全合格的namespace和class必须符合这样的结构：“\&lt; Vendor Name&gt;(&lt; Namespace&gt;)*&lt; Class Name&gt;”</li>
<li>每个namespace必须有一个顶层的namespace（”Vendor Name”提供者名字）</li>
<li>每个namespace可以有多个子namespace</li>
<li>当从文件系统中加载时，每个namespace的分隔符(/)要转换成 DIRECTORY_SEPARATOR(操作系统路径分隔符)</li>
<li>在类名中，每个下划线(_)符号要转换成DIRECTORY_SEPARATOR(操作系统路径分隔符)。在namespace中，下划线(_)符号是没有（特殊）意义的。</li>
<li>当从文件系统中载入时，合格的namespace和class一定是以 .php 结尾的</li>
<li>verdor name,namespaces,class名可以由大小写字母组合而成（大小写敏感的）</li>
</ul>
</blockquote>
<hr>
<p>是不是有点看不懂啊。什么namespace啊，什么autoloading啊。所以，如果你对命名空间还不是特别懂的话，可以google下namespace 和 自动加载相关的php知识。或者看下一篇，我专门来讲讲他们：namespace 和 autoloading<br>好，我们接着一条条举例来分析下：</p>
<h3 id="0-1-第1条"><a href="#0-1-第1条" class="headerlink" title="0.1 第1条"></a>0.1 第1条</h3><p>比如我的文件 /Doctrine/Common/IsolatedClassLoader.php 这样子的一个类文件，那么你的namespace命名就必须声明称这样：</p>
<blockquote>
<p>声明：namespace  \Doctrine\Common\<br>调用：\Doctrine\Common\IsolatedClassLoader</p>
</blockquote>
<hr>
<p>其中，Doctrine 表示一个模块目录 Vendor name, common就是namesapce, IsolatedClassLoader是class name。这样一看就知道这个文件的目录层次，一目了然。<br>再比如：/path/to/project/lib/vendor/Symfony/Core/Request.php 文件：</p>
<blockquote>
<p>声明：namespace \Symfony\Core<br>调用：\Symfony\Core\Request</p>
</blockquote>
<hr>
<h3 id="0-2-0-3-第2，3条"><a href="#0-2-0-3-第2，3条" class="headerlink" title="0.2-0.3 第2，3条"></a>0.2-0.3 第2，3条</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">namespace \Zend\Acl =&gt; /path/to/project/lib/vendor/Zend/Acl.php</span><br><span class="line">namespace \Zend\Mail\Message =&gt; /path/to/project/lib/vendor/Zend/Mail/Message.php</span><br></pre></td></tr></table></figure>
<p>必须有一个顶级的zend的namespace, zend 下面可以有message子命名空间。</p>
<h3 id="0-4-第4条"><a href="#0-4-第4条" class="headerlink" title="0.4 第4条"></a>0.4 第4条</h3><p>看这个例子，我们需要new 一个这样的类<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> \Symfony\Core\Request</span><br></pre></td></tr></table></figure></div></p>
<p>那么我再加载这个类文件时候，就要将分隔符 \ 转换成 目录，也就是去 Vendor -&gt; Symfony-&gt;Core-&gt;Request.php 一层层的目录找到这个文件。其实也就是和第1,2,3是反过来的对应关系。</p>
<h3 id="0-5-第5条"><a href="#0-5-第5条" class="headerlink" title="0.5 第5条"></a>0.5 第5条</h3><p>第5条是说<code>namespace</code>命名中的这个_ 符号 没有任何用处，就是用来表示目录分隔符的，但是注意在PRS-4中已经取消了这个_ ，那么我们还是看一下，这个过时的规定是怎么样的：<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">\namespace\package\Class_Name =&gt; /path/to/project/lib/vendor/namespace/package/Class/Name.php</span><br><span class="line">\namespace\package_name\Class_Name =&gt; /path/to/project/lib/vendor/namespace/package_name/Class/Name.php</span><br></pre></td></tr></table></figure></p>
<p>以上2个namespace中的 _ 其实是目录分隔符。并不是class name就是那样的。现在PSR-4出来后，确实觉得这样的规定有点不好。怪怪的。</p>
<h3 id="0-6-第6条"><a href="#0-6-第6条" class="headerlink" title="0.6 第6条"></a>0.6 第6条</h3><p>第6条不用多说。既然你用<code>php</code>，文件名当然是得用<code>.php</code> 后缀结尾。这个规定的原因，我大致猜一下，估计是有人用 <code>.php3</code> 后缀的。好吧。这个是个神话了。因为在apache的配置文件中是允许的：<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="APACHE"><figure class="iseeu highlight /apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">&lt;IfModule dir_module&gt;</span></span><br><span class="line">    <span class="attribute">DirectoryIndex</span> index.php index.php3 index.html index.htm</span><br><span class="line"><span class="section">&lt;/IfModule&gt;</span></span><br></pre></td></tr></table></figure></div></p>
<p>所以，老老实实的用.php作为后缀名吧。</p>
<h3 id="0-7-第7条"><a href="#0-7-第7条" class="headerlink" title="0.7 第7条"></a>0.7 第7条</h3><p>文件大小写的问题，这个其实是很重要的。因为Linux系统下是区分文件名和目录名大小写的，而在Windows下是不区分的。所以就会经常出现问题，比如：<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">namespace</span>  \<span class="title">Doctrine</span>\<span class="title">Common</span>\<span class="title">IsolatedClassLoader</span></span><br></pre></td></tr></table></figure></div></p>
<p>在Linux下就去严格按照大小写去找目录和文件了。但是如果你在Windows下开发，全是小写也不会报错，你一发布到Linux上就悲剧了，提示找不到文件。所以，大小写太重要了。</p>
<h2 id="1-0-PSR-1-规范"><a href="#1-0-PSR-1-规范" class="headerlink" title="1.0 PSR-1 规范"></a>1.0 PSR-1 规范</h2><p>还是那句话，如果你英文比较好，可以直接看官网的这个：PSR-1，也不长，基本能看懂。但是如果你英语不好，那么还是请继续往下看</p>
<blockquote>
<p>-<code>PHP</code>源文件必须只使用 &lt;?php 和 &lt;?= 这两种标签</p>
<ul>
<li>源文件中php代码的编码格式必须是不带字节顺序标记(BOM)的UTF-8。</li>
<li>一个源文件建议只用来做声明（类(class)，函数(function)，常量(constant)等）或者只用来做一些引起副作用的操作（例如：输出信息，修改.ini配置等），但不建议同时做这两件事。</li>
<li>命名空间(namespace)和类(class) 必须遵守PSR-0标准。</li>
<li>类名(class name) 必须使用骆驼式(StudlyCaps)写法 (注：驼峰式(cameCase)的一种变种，后文将直接用StudlyCaps表示)。</li>
<li>类(class)中的常量必须只由大写字母和下划线(_)组成。</li>
<li>方法名(method name) 必须使用驼峰式(cameCase)写法。</li>
</ul>
</blockquote>
<hr>
<p>好，就是上面的基本7大点，有些很简单，就不过多说明，第3点需要仔细说下。</p>
<h3 id="1-1-第1条"><a href="#1-1-第1条" class="headerlink" title="1.1 第1条"></a>1.1 第1条</h3><p>这个基本都是大家都懂得的，<code>PHP</code>代码必须只使用长标签<code>(&lt;?php ?&gt;)</code>或者短输出式标签<code>(&lt;?= ?&gt;)</code>；而不要使用其他标签。<br>之所以说这个，是因为我们在学习<code>PHP</code>的时候，一般的教材都告诉我们说，php有4种标记风格：<code>php</code>的4种标记风格 所以，很多人就乱来了。特别是<code>asp</code>风格的兼职是折磨好么！！！</p>
<h3 id="1-2-第2条"><a href="#1-2-第2条" class="headerlink" title="1.2 第2条"></a>1.2 第2条</h3><p>这个不多说，保存的时候格式必须是<code>无BOM的UTF-8</code>格式，否则会出现很多无法解释的千奇百怪的问题。千万别傻逼的用Windows下的text文本编辑器保存文件啊</p>
<h3 id="2-3-第3条"><a href="#2-3-第3条" class="headerlink" title="2.3 第3条"></a>2.3 第3条</h3><p>这个通俗的说呢。就是别把一些输出和修改的操作(副作用) 和 类文件混合在一起，专注一点，这个文件专门来声明<code>Class</code>, 那个文件专门来修改配置文件，别混在一起写：<br>所以，以下的这个文件是有问题的，最好不要这样：<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 副作用：修改了ini配置</span></span><br><span class="line">ini_set(<span class="string">'error_reporting'</span>, E_ALL);</span><br><span class="line"> </span><br><span class="line"><span class="comment">// 副作用：载入了文件</span></span><br><span class="line"><span class="keyword">include</span> <span class="string">"file.php"</span>;</span><br><span class="line"> </span><br><span class="line"><span class="comment">// 副作用：产生了输出</span></span><br><span class="line"><span class="keyword">echo</span> <span class="string">"&lt;html&gt;\n"</span>;</span><br><span class="line"> </span><br><span class="line"><span class="comment">// 声明 function</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 函数体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p>你看上面看起来多乱啊。最好全部分开来写:<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">namespace</span> <span class="title">Lib</span>;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Name</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">echo</span> <span class="keyword">__NAMESPACE__</span> . <span class="string">"&lt;br&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">echo</span> <span class="keyword">__NAMESPACE__</span> . <span class="string">' static function test &lt;br&gt;'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p><em>修改ini:</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ini_set(<span class="string">'error_reporting'</span>, E_ALL);</span><br></pre></td></tr></table></figure></div></p>
<p><em>require 文件：</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">require</span> DIR . <span class="string">'/loading.php'</span>;</span><br><span class="line">spl_autoload_register(<span class="string">"\\AutoLoading\\loading::autoload"</span>);</span><br></pre></td></tr></table></figure></div></p>
<p>你看是不是整齐好看多了。当然这个是很难约束的。自己仔细划分。</p>
<h3 id="1-4-第4条"><a href="#1-4-第4条" class="headerlink" title="1.4 第4条"></a>1.4 第4条</h3><p>第4条是约束namespace的，前面已经说过，不多说。值得说的是名字要是驼峰方式来</p>
<h3 id="1-5-第5条"><a href="#1-5-第5条" class="headerlink" title="1.5 第5条"></a>1.5 第5条</h3><p>class name必须要用驼峰方式写，驼峰又分小驼峰和大驼峰（小驼峰是第一个字母是小写）这样写看着舒服也比较规范，不做要求，反正是驼峰就可以了。我喜欢用小驼峰：<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">getUserInfo</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<h3 id="1-6-第6条"><a href="#1-6-第6条" class="headerlink" title="1.6 第6条"></a>1.6 第6条</h3><p>是规定类中的常量名(const)声明必须要全部大写，如果有多个单词，就用_分开：<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">getUserInfo</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// 全部大写</span></span><br><span class="line">    <span class="keyword">const</span> NAME = <span class="string">'phper'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 用_隔开</span></span><br><span class="line">    <span class="keyword">const</span> HOUSE_INFO = <span class="string">'已经深圳买房'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getUserName</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<h3 id="第7条"><a href="#第7条" class="headerlink" title="第7条"></a>第7条</h3><p>method name必须要用驼峰方式写，大小驼峰都可以，不做要求，我喜欢用小驼峰：<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">getUserInfo</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getUserName</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<h2 id="PRSR-2-规范"><a href="#PRSR-2-规范" class="headerlink" title="PRSR-2 规范"></a><em>PRSR-2 规范</em></h2><p><code>PSR-2</code> 规范的官网链接在此：<code>PSR-2</code> 这一规范主要是约束代码风格的，可是说是所有里面最关键最重要的，也是需要好好规范和共同遵守的。<br>我们一个个来看下，只能我大略的写一些比较重要的，或者说平时用的最多的。</p>
<h3 id="2-1-源文件"><a href="#2-1-源文件" class="headerlink" title="2.1 源文件"></a>2.1 源文件</h3><blockquote>
<ul>
<li>文件末尾必须空一行。</li>
<li>必须使用Unix LF(换行)作为行结束符。</li>
<li>纯PHP代码源文件的关闭标签?&gt;必须省略。</li>
</ul>
</blockquote>
<hr>
<p>第3点其实是蛮重要的，我之前还老写闭合，现在不写了。这可以避免在 <code>PHP</code> 结束标记之后万一意外加入了空格或者换行符，会导致 PHP 开始输出这些空白，而脚本中此时并无输出的意图。</p>
<h3 id="2-2-缩进"><a href="#2-2-缩进" class="headerlink" title="2.2 缩进"></a>2.2 缩进</h3><p>必须使用4个空格来缩进，不能使用<code>Tab</code>键。当然你如果把Tab在编辑器里手动设置为4个空格也可以。这样的目的是因为：每个人的机器上的Tab键都不一样，有些是4个空格，有些是8个空格，在你的机器上看着很爽的代码，在别人机器上了就各种恶心了。所以，统一搞成4个空格，不管在哪里打开都是美观的。</p>
<h3 id="2-3-行"><a href="#2-3-行" class="headerlink" title="2.3 行"></a>2.3 行</h3><p>一行推荐的是最多写80个字符，多于这个字符就应该换行了，一般的编辑器是可以设置的。</p>
<h3 id="2-4-关键字和-True-False-Null"><a href="#2-4-关键字和-True-False-Null" class="headerlink" title="2.4 关键字和 True/False/Null"></a>2.4 关键字和 True/False/Null</h3><p><code>php</code>的关键字，必须小写，<code>boolean</code>值：<code>true</code>，<code>false</code>，<code>null</code>也必须小写<br>下面是<code>php</code>的<code>keyword</code>，必须小写。</p>
<blockquote>
<p>‘__halt_compiler’, ‘abstract’, ‘and’, ‘array’, ‘as’, ‘break’, ‘callable’, ‘case’, ‘catch’, ‘class’, ‘clone’, ‘const’, ‘continue’, ‘declare’, ‘default’, ‘die’, ‘do’, ‘echo’, ‘else’, ‘elseif’, ‘empty’, ‘enddeclare’, ‘endfor’, ‘endforeach’, ‘endif’, ‘endswitch’, ‘endwhile’, ‘eval’, ‘exit’, ‘extends’, ‘final’, ‘for’, ‘foreach’, ‘function’, ‘global’, ‘goto’, ‘if’, ‘implements’, ‘include’, ‘include_once’, ‘instanceof’, ‘insteadof’, ‘interface’, ‘isset’, ‘list’, ‘namespace’, ‘new’, ‘or’, ‘print’, ‘private’, ‘protected’, ‘public’, ‘require’, ‘require_once’, ‘return’, ‘static’, ‘switch’, ‘throw’, ‘trait’, ‘try’, ‘unset’, ‘use’, ‘var’, ‘while’, ‘xor’</p>
</blockquote>
<hr>
<h3 id="2-5-命名空间-Namespace-和导入-Use-声明"><a href="#2-5-命名空间-Namespace-和导入-Use-声明" class="headerlink" title="2.5 命名空间(Namespace)和导入(Use)声明"></a>2.5 命名空间(Namespace)和导入(Use)声明</h3><p>先简单文字描述下：</p>
<ul>
<li>命名空间(<code>namespace</code>)的声明后面必须有一行空行。</li>
<li>所有的导入(<code>use</code>)声明必须放在命名空间(<code>namespace</code>)声明的下面。</li>
<li>一句声明中，必须只有一个导入(<code>use</code>)关键字。</li>
<li>在导入(<code>use</code>)声明代码块后面必须有一行空行。<br><em>用代码来说明下：</em>  </li>
</ul>
<div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Lib</span>\<span class="title">Databases</span>; <span class="comment">// 下面必须空格一行</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mysql</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p><em><code>namespace</code>下空一行，才能使用<code>use</code>，再空一行，才能声明<code>class</code></em></p>
<div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Lib</span>\<span class="title">Databases</span>; <span class="comment">// 下面必须空格一行</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">use</span> <span class="title">FooInterface</span>; <span class="comment">// use 必须在namespace 后面声明</span></span><br><span class="line"><span class="keyword">use</span> <span class="title">BarClass</span> <span class="title">as</span> <span class="title">Bar</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">OtherVendor</span>\<span class="title">OtherPackage</span>\<span class="title">BazClass</span>; <span class="comment">// 下面必须空格一行</span></span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mysql</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<h3 id="2-6-类-class-，属性-property-和方法-method"><a href="#2-6-类-class-，属性-property-和方法-method" class="headerlink" title="2.6 类(class)，属性(property)和方法(method)"></a>2.6 类(<code>class</code>)，属性(<code>property</code>)和方法(<code>method</code>)</h3><p>（1）、继承(<code>extends</code>) 和实现(<code>implement</code>) 必须和 <code>class</code> <code>name</code> 写在一行，切花括号要换行写。<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Lib</span>\<span class="title">Databaes</span>;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mysql</span> <span class="keyword">extends</span> <span class="title">ParentClass</span> <span class="keyword">implements</span> \<span class="title">PDO</span>, \<span class="title">DB</span> // 写一行</span></span><br><span class="line"><span class="class"></span>&#123; <span class="comment">// 换行写&#123;</span></span><br><span class="line">     </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p>（2）、属性(<code>property</code>)必须声明其可见性，到底是 <code>public</code> 还是 <code>protected</code> 还是 <code>private</code>，不能省略，也不能使用<code>var</code>, <code>var</code>是<code>php</code>老版本中的什么方式，等用于<code>public</code>。<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Lib</span>\<span class="title">Databaes</span>;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mysql</span> <span class="keyword">extends</span> <span class="title">ParentClass</span> <span class="keyword">implements</span> \<span class="title">PDO</span>, \<span class="title">DB</span> // 写一行</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> $foo = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">private</span> $name = <span class="string">'yangyi'</span>;</span><br><span class="line">    <span class="keyword">protected</span> $age = <span class="string">'17'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p>（3）、方法(method)，必须声明其可见性，到底是public还是protected还是private，不能省略。并且，花括号{必须换行写。如果有多个参数，第一个参数后紧接,,再加个空格，且函数name和(之间必须要有个空格：<code>function_name($par,$par2,$pa3)</code>,如果参数有默认值，也要用左右空格分开。<br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Lib</span>\<span class="title">Databaes</span>;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mysql</span> <span class="keyword">extends</span> <span class="title">ParentClass</span> <span class="keyword">implements</span> \<span class="title">PDO</span>, \<span class="title">DB</span> // 写一行</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> getInfo ($name, $age, $gender = <span class="number">1</span>) <span class="comment">// 函数名getInfo和(之间有个空格，参数之间也要有空格。默认参数也要左右都有空格</span></span><br><span class="line">    &#123; <span class="comment">// 必须换行写 &#123;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p>（4）、当用到抽象(<code>abstract</code>)和终结(<code>final</code>)来做类声明时，它们必须放在可见性声明 （<code>public</code> 还是<code>protected</code>还是<code>private</code>）的前面。而当用到静态(<code>static</code>)来做类声明时，则必须放在可见性声明的后面。<br><em>直接上代码：</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Vendor</span>\<span class="title">Package</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">ClassName</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> $foo; <span class="comment">// static放后面</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">abstract</span> <span class="keyword">protected</span> <span class="function"><span class="keyword">function</span> <span class="title">zim</span><span class="params">()</span></span>; <span class="comment">// abstract放前面</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">bar</span><span class="params">()</span> // <span class="title">final</span>放前面，<span class="title">static</span>放最后。</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 方法主体部分</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<h3 id="2-7-控制结构"><a href="#2-7-控制结构" class="headerlink" title="2.7 控制结构"></a>2.7 控制结构</h3><p><em>控制接口，就是 <code>if else while switch</code>等。这一类的写法规范也是经常容易出现问题的，也要规范一下</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">if</span> ($expr1) &#123; <span class="comment">// 左右空格</span></span><br><span class="line">    <span class="comment">// if body</span></span><br><span class="line">&#125; <span class="keyword">elseif</span> ($expr2) &#123; <span class="comment">// elesif 连着写</span></span><br><span class="line">    <span class="comment">// elseif body</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// else body;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p><em>（2）、switch，case 注意左右空格和换行，还是直接上规范代码</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">switch</span> ($expr) &#123; <span class="comment">// 左右空格</span></span><br><span class="line">    <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">echo</span> <span class="string">'First case, with a break'</span>; <span class="comment">// 对齐</span></span><br><span class="line">        <span class="keyword">break</span>; <span class="comment">// 换行写break，也对齐。</span></span><br><span class="line">    <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">        <span class="keyword">echo</span> <span class="string">'Second case, which falls through'</span>;</span><br><span class="line">        <span class="comment">// no break</span></span><br><span class="line">    <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">    <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">    <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">        <span class="keyword">echo</span> <span class="string">'Third case, return instead of break'</span>;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        <span class="keyword">echo</span> <span class="string">'Default case'</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p><em>（3）、<code>while</code>，<code>do</code> <code>while</code> 的写法也是类似，要左右空格，上代码：</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">while</span> ($expr) &#123; <span class="comment">// 左右空格</span></span><br><span class="line">    <span class="comment">// structure body</span></span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">do</span> &#123;</span><br><span class="line">    <span class="comment">// structure body; // 左右空格</span></span><br><span class="line">&#125; <span class="keyword">while</span> ($expr);</span><br></pre></td></tr></table></figure></div></p>
<p><em>（4）、<code>for</code>的写法</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">10</span>; $i++) &#123; <span class="comment">// 注意几个参数之间的空格</span></span><br><span class="line">    <span class="comment">// for body</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p><em>（5）、<code>foreach</code> 的写法</em><br><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">foreach</span> ($iterable <span class="keyword">as</span> $key =&gt; $value) &#123; <span class="comment">// 还是空格问题</span></span><br><span class="line">    <span class="comment">// foreach body</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p> <em>（6）、<code>try catch</code>的写法</em><br> <div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PHP"><figure class="iseeu highlight /php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> <span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="comment">// try body</span></span><br><span class="line">&#125; <span class="keyword">catch</span> (FirstExceptionType $e) &#123; <span class="comment">// 同样也是注意空格。</span></span><br><span class="line">    <span class="comment">// catch body</span></span><br><span class="line">&#125; <span class="keyword">catch</span> (OtherExceptionType $e) &#123;</span><br><span class="line">    <span class="comment">// catch body</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></p>
<p>基本用到的就是这些了，其他什么闭包啥的用的不多就不过多的累述了。</p>
<h2 id="3-0-PSR-3规范"><a href="#3-0-PSR-3规范" class="headerlink" title="3.0 PSR-3规范"></a>3.0 <code>PSR-3</code>规范</h2><p>PSR-3规范主要是来规范日志接口(Logger Interface)的，老实讲，其实平常接触的不是特别多，所以就不说了，可以去看官网的<br><span class="exturl" data-url="aHR0cDovL3d3dy5waHAtZmlnLm9yZy9wc3IvcHNyLTMv" title="http://www.php-fig.org/psr/psr-3/">PSR-3<i class="fa fa-external-link"></i></span></p>
<h2 id="PSR-4规范"><a href="#PSR-4规范" class="headerlink" title="PSR-4规范"></a><code>PSR-4</code>规范</h2><p><code>PSR-4</code>规范是刚出没多久的一条新的规范，它也是规范 自动加载(<code>autoload</code>)的，是对<code>PSR</code>-0的修改，属于补充规范，</p>
<blockquote>
<ul>
<li>我简单说下，主要是以下几点： </li>
<li>废除了PSR-0中<em>就是目录分割符的写法，</em>下划线在完全限定类名中是没有特殊含义了。 </li>
<li>类文件名要以 .php 结尾。 </li>
<li>类名必须要和对应的文件名要一模一样，大小写也要一模一样。</li>
</ul>
</blockquote>
<hr>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuenlidWx1by5jb20vcGhwZXIvbm90ZS82NTAzMw==" title="https://www.zybuluo.com/phper/note/65033">https://www.zybuluo.com/phper/note/65033<i class="fa fa-external-link"></i></span><br><span class="exturl" data-url="aHR0cDovL3NlZ21lbnRmYXVsdC5jb20vYS8xMTkwMDAwMDAwMzgwMDA4" title="http://segmentfault.com/a/1190000000380008">http://segmentfault.com/a/1190000000380008<i class="fa fa-external-link"></i></span><br><span class="exturl" data-url="aHR0cDovL3d3dy40d2VpLmNuL2FyY2hpdmVzLzEwMDIxODY=" title="http://www.4wei.cn/archives/1002186">http://www.4wei.cn/archives/1002186<i class="fa fa-external-link"></i></span><br><span class="exturl" data-url="aHR0cDovL3dlbmt1LmJhaWR1LmNvbS92aWV3LzdhMjFlNDRiNDhkN2MxYzcwOGExNDU3Ny5odG1sIzEwMDAyLXRzaW5hLTEtODc4NDMtZTI5YjQ3ODRlZGE1ZDFmNTFmYjBjMmE5N2ExNWRhMDg=" title="http://wenku.baidu.com/view/7a21e44b48d7c1c708a14577.html#10002-tsina-1-87843-e29b4784eda5d1f51fb0c2a97a15da08">http://wenku.baidu.com/view/7a21e44b48d7c1c708a14577.html#10002-tsina-1-87843-e29b4784eda5d1f51fb0c2a97a15da08<i class="fa fa-external-link"></i></span><br><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hmY29ycmllei9maWctc3RhbmRhcmRz" title="https://github.com/hfcorriez/fig-standards">https://github.com/hfcorriez/fig-standards<i class="fa fa-external-link"></i></span></p>
<p>声明：namespace  \Doctrine\Common\<br>调用：\Doctrine\Common\IsolatedClassLoader<br><span class="exturl" data-url="aHR0cHM6Ly9sZWFybmt1LmNvbS9kb2NzL3Bzcg==" title="https://learnku.com/docs/psr">中文翻译文档<i class="fa fa-external-link"></i></span></p>

      
    </div>

    

    
    
    

    

    
      
    
    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/uploads/pay/wechatpay.png" alt="吴楚衡 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/uploads/pay/alipay.png" alt="吴楚衡 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/php/" rel="tag"># php</a>
          
            <a href="/tags/PSR规范/" rel="tag"># PSR规范</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/11/28/【php】php常用库和前端常用组件库(转载)/" rel="next" title="「php」php常用库和前端常用组件库(转载)">
                <i class="fa fa-chevron-left"></i> 「php」php常用库和前端常用组件库(转载)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/14/【linux】鸽舍笔记-怎么搭建邮件服务器/" rel="prev" title="【linux】鸽舍笔记-怎么搭建邮件服务器">
                【linux】鸽舍笔记-怎么搭建邮件服务器 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/favicon.ico" alt="吴楚衡">
            
              <p class="site-author-name" itemprop="name">吴楚衡</p>
              <p class="site-description motion-element" itemprop="description">IT菜鸟,写这个博客是防止未来把自己学习的东西给忘记了,也是为了自己学习的那点东西做下总结加深下印象,请多指教^_^!</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">99</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">16</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">76</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3d1Y2h1aGVuZw==" title="GitHub &rarr; https://github.com/wuchuheng"><i class="fa fa-fw fa-github"></i>GitHub</span>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="bWFpbHRvOmFkbWluQHd1Y2h1aGVuZy5jb20=" title="E-Mail &rarr; mailto:admin@wuchuheng.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
                </span>
              
            </div>
          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL3poLWNuL2RvY3MvaW5kZXguaHRtbA==" title="https://hexo.io/zh-cn/docs/index.html">hexo文档</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cDovL3RoZW1lLW5leHQuaWlzc25hbi5jb20=" title="http://theme-next.iissnan.com">主题文档</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cDovL3d3dy5saW51eGV5ZS5jb20=" title="http://www.linuxeye.com">linux学习平台</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9naXQtc2NtLmNvbS9ib29rL3poL3Yy" title="https://git-scm.com/book/zh/v2">git使用手册</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9oaWdobGlnaHRqcy5vcmc=" title="https://highlightjs.org">highlightjs</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9odWIuZG9ja2VyLmNvbS91L3d1Y2h1aGVuZy8=" title="https://hub.docker.com/u/wuchuheng/">docker</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cDovL3hpYW9oYW55dS5tZQ==" title="http://xiaohanyu.me">cnlox</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cDovL3hpYW9oYW55dS5tZQ==" title="http://xiaohanyu.me">vim从入门到精通</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9jaWtlYmxvZy5jb20v" title="https://cikeblog.com/">刺客博客</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9oeWRpbmdzeS5naXRodWIuaW8v" title="https://hydingsy.github.io/">算法牛人</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cDovL3hhcmdpbi5jb20vcmVhZGluZ3Mv" title="http://xargin.com/readings/">牛人</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9lYm9va2ZvdW5kYXRpb24uZ2l0aHViLmlvL2ZyZWUtcHJvZ3JhbW1pbmctYm9va3Mv" title="https://ebookfoundation.github.io/free-programming-books/">githhub开源书</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9sYWlqdy5jb20v" title="https://laijw.com/">赖经纬</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly93dzRrLmNvbS8=" title="https://ww4k.com/">徐庆业</span>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <span class="exturl" data-url="aHR0cHM6Ly9oYXNhaWsuY29tLw==" title="https://hasaik.com/">next主题参考</span>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#PHP-FIG"><span class="nav-text">PHP-FIG</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0-0-PSR-0-规范"><span class="nav-text">0.0 PSR-0 规范</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#0-1-第1条"><span class="nav-text">0.1 第1条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#0-2-0-3-第2，3条"><span class="nav-text">0.2-0.3 第2，3条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#0-4-第4条"><span class="nav-text">0.4 第4条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#0-5-第5条"><span class="nav-text">0.5 第5条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#0-6-第6条"><span class="nav-text">0.6 第6条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#0-7-第7条"><span class="nav-text">0.7 第7条</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-0-PSR-1-规范"><span class="nav-text">1.0 PSR-1 规范</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-第1条"><span class="nav-text">1.1 第1条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-第2条"><span class="nav-text">1.2 第2条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-第3条"><span class="nav-text">2.3 第3条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-第4条"><span class="nav-text">1.4 第4条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-第5条"><span class="nav-text">1.5 第5条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6-第6条"><span class="nav-text">1.6 第6条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#第7条"><span class="nav-text">第7条</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#PRSR-2-规范"><span class="nav-text">PRSR-2 规范</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-源文件"><span class="nav-text">2.1 源文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-缩进"><span class="nav-text">2.2 缩进</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-行"><span class="nav-text">2.3 行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-关键字和-True-False-Null"><span class="nav-text">2.4 关键字和 True/False/Null</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-命名空间-Namespace-和导入-Use-声明"><span class="nav-text">2.5 命名空间(Namespace)和导入(Use)声明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-类-class-，属性-property-和方法-method"><span class="nav-text">2.6 类(class)，属性(property)和方法(method)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7-控制结构"><span class="nav-text">2.7 控制结构</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-0-PSR-3规范"><span class="nav-text">3.0 PSR-3规范</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#PSR-4规范"><span class="nav-text">PSR-4规范</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考资料"><span class="nav-text">参考资料</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">  <span class="exturl" data-url="aHR0cDovL3d3dy5taWl0YmVpYW4uZ292LmNu">粤ICP备17071471号 </span>&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">吴楚衡</span>

  

  
</div>


  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> 强力驱动 v3.9.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Mist</span> v6.7.0</div>





  <script src="https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js"></script>
    <script>
        if (window.mermaid) {
            mermaid.initialize("");
        }
  </script>



        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="总访客量">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="post-meta-divider">|</span>
  

  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="总访问量">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=6.7.0"></script>

  <script src="/js/src/motion.js?v=6.7.0"></script>



  
  


  <script src="/js/src/schemes/muse.js?v=6.7.0"></script>




  
  <script src="/js/src/scrollspy.js?v=6.7.0"></script>
<script src="/js/src/post-details.js?v=6.7.0"></script>



  


  <script src="/js/src/bootstrap.js?v=6.7.0"></script>



  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: 'vUPCRqeu7bXvUdFGGLuT3zvs-gzGzoHsz',
    appKey: 'cooQBOkOaV3XE3SP4ihtaWOe',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false
  });
</script>




  


  

  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  

  

  

  

  

  
  <script src="/js/src/exturl.js?v=6.7.0"></script>


  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function(i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap');
      $(e).after($wrap);
      $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
        var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
          return $(e).text();
        }).toArray().join('\n');
        var ta = document.createElement('textarea');
        var range = document.createRange(); //For Chrome
        var sel = window.getSelection(); //For Chrome
        var yPosition = window.pageYOffset || document.documentElement.scrollTop;
        ta.style.top = yPosition + 'px'; //Prevent page scroll
        ta.style.position = 'absolute';
        ta.style.opacity = '0';
        ta.value = code;
        ta.textContent = code; //For FireFox
        ta.contentEditable = true;
        ta.readOnly = false;
        document.body.appendChild(ta);
        range.selectNode(ta);
        sel.removeAllRanges();
        sel.addRange(range);
        ta.setSelectionRange(0, code.length);
        var result = document.execCommand('copy');
        
          if (result) $(this).text('复制成功');
          else $(this).text('复制失败');
        
        ta.blur(); //For iOS
        $(this).blur();
      })).on('mouseleave', function(e) {
        var $b = $(this).find('.copy-btn');
        setTimeout(function() {
          $b.text('复制');
        }, 300);
      }).append(e);
    })
  </script>


  

</body>
</html>
